package nm.util.alarmclock;

import java.io.Serializable;
import java.util.Calendar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class AlarmSettings implements Serializable {
	public static final long serialVersionUID = 1L;
	public static final String HOME_DIR = 
		System.getProperty("user.home") +
		System.getProperty("file.separator");
	public static final String SETTINGS_FILE = AlarmSettings.HOME_DIR + 
		".alarmclockrc";
	public static final int HOUR_MIN = 0;
	public static final int HOUR_MAX = 23;
	public static final int HOUR_DEFAULT = 0;
	public static final int MINUTE_MIN = 0;
	public static final int MINUTE_MAX = 59;
	public static final int MINUTE_DEFAULT = 0;
	
	private AlarmSettings(Calendar cal) {
		hour = cal.get(Calendar.HOUR_OF_DAY);
		minute = cal.get(Calendar.MINUTE);
		second = cal.get(Calendar.SECOND);
		sound = null;
	}
	
	public boolean equals(Object o) {
		if(o instanceof AlarmSettings)
		{
			AlarmSettings alarm = (AlarmSettings)o;
			if(this.getHour() == alarm.getHour())
				if(this.getMinute() == alarm.getMinute())
					if(alarm.getSound() != null)
						return this.getSound().equals(alarm.getSound());
		}
		return false;
	}
	
	public int getHour() {
		return hour;
	}
	public int getMinute() {
		return minute;
	}
	public int getSecond() {
		return second;
	}
	public File getSound() {
		return sound;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}
	public void setMinute(int minute) {
		this.minute = minute;
	}
	public void setSecond(int second) {
		this.second = second;
	}
	public void setSound(File sound) {
		this.sound = sound;
	}

	public boolean save() {
		File s = new File(AlarmSettings.SETTINGS_FILE);
		if(s.exists()) s.delete();
		try
		{
			ObjectOutputStream output = new ObjectOutputStream(
					new FileOutputStream(s));
			output.writeObject(this);
			output.close();
			return true;
		}
		catch(java.io.FileNotFoundException fnf)
		{
			// file not found, open a new one.
			return false;
		}
		catch(java.io.IOException ioe)
		{
			// unable to write object
			// don't save settings
			return false;
		}
	}
	
	public static AlarmSettings getSettings() {
		File s = new File(AlarmSettings.SETTINGS_FILE);
		AlarmSettings settings;
		try
		{
			// read old settings
			ObjectInputStream input = new ObjectInputStream(
					new FileInputStream(s));
			settings = (AlarmSettings)input.readObject();
			input.close();
			return settings;
		}
		catch(java.io.FileNotFoundException fnf)
		{
			// create new settings
			settings = new AlarmSettings(Calendar.getInstance());
			return settings;
		}
		catch(java.io.IOException ioe)
		{
			// unable to read object from stream
			settings = new AlarmSettings(Calendar.getInstance());
			return settings;
		}
		catch(ClassNotFoundException cnf)
		{
			// unable to fetch AlarmSettings
			settings = new AlarmSettings(Calendar.getInstance());
			return settings;
		}
	}
	
	//instance variables
	private int hour, minute, second;
	private File sound;

}//end AlarmSettings
